<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>心理测评</el-breadcrumb-item>
    <el-breadcrumb-item>测评题库管理</el-breadcrumb-item>
  </el-breadcrumb>
  <el-form :inline="true" :model="queryInfo" class="demo-form-inline">
    <el-form-item label="测评名">
      <el-input v-model="queryInfo.problemName" placeholder="请输入测评名" clearable @clear="queryDataList" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="queryDataList">搜索</el-button>
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="danger" @click="batchRemove">批量删除</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="dataList" border row-key="id" style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column type="selection" fixed="left" width="40" />
    <el-table-column property="id" label="ID" fixed="left" />
    <el-table-column property="problemName" label="测评名" />
    <el-table-column property="mindTestId" label="心理测评类别">
      <template #default="scope">
        <el-tag type="primary" v-if="scope.row.mindTestId === 1">亲密关系</el-tag>
        <el-tag type="primary" v-if="scope.row.mindTestId === 2">性格人格</el-tag>
        <el-tag type="primary" v-if="scope.row.mindTestId === 3">情绪管理</el-tag>
        <el-tag type="primary" v-if="scope.row.mindTestId === 4">原生家庭</el-tag>
        <el-tag type="primary" v-if="scope.row.mindTestId === 5">职场能力</el-tag>
        <el-tag type="primary" v-if="scope.row.mindTestId === 6">心理健康</el-tag>
        <el-tag type="primary" v-if="scope.row.mindTestId === 7">压力评估</el-tag>
        <el-tag type="primary" v-if="scope.row.mindTestId === 8">人际关系</el-tag>
        <el-tag type="primary" v-if="scope.row.mindTestId === 9">自我认知</el-tag>
        <el-tag type="primary" v-if="scope.row.mindTestId === 10">学习能力</el-tag>
      </template>
    </el-table-column>
    <!-- 	<el-table-column property="aboutTest" label="关于测评" />
      <el-table-column property="willGain" label="你将获得" />
      <el-table-column property="suitPlan" label="适合人群" />
      <el-table-column property="references" label="参考文献" />
      <el-table-column property="originPrice" label="原价" />
      <el-table-column property="discountPrice" label="折扣价" /> -->
    <el-table-column fixed="right" label="操作" min-width="120">
      <template #default="scope">
        <el-button type="primary" size="small" @click="edit(scope.row)">编辑</el-button>
        <el-button type="danger" size="small" @click="remove(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination v-model:current-page="queryInfo.pageNum" v-model:page-size="queryInfo.pageSize"
                 :page-sizes="[5, 10, 15, 20]" size="default" layout="total, sizes, prev, pager, next, jumper" :total="total"
                 @size-change="handleSizeChange" @current-change="handleCurrentChange" />

  <!-- 新增和修改对话框 -->
  <el-dialog :title="title" v-model="dialogFormVisible" width="950" @close="cancle">
    <el-form :model="testData" label-width="100">
      <el-form-item label="测评名">
        <el-input v-model="testData.problemName" autocomplete="off" placeholder="请输入测评名"></el-input>
      </el-form-item>
      <!-- 		<el-form-item label="测评简介">
            <el-input v-model="testData.intro" placeholder="请输入测评简介"></el-input> -->
      <!-- </el-form-item> -->
      <el-form-item label="心理测评类别">
        <el-select v-model="testData.mindTestId" placeholder="心理测评类别id">
          <el-option label="亲密关系" value="1" />
          <el-option label="性格人格" value="2" />
          <el-option label="情绪管理" value="3" />
          <el-option label="原生家庭" value="4" />
          <el-option label="职场能力" value="5" />
          <el-option label="心理健康" value="6" />
          <el-option label="压力评估" value="7" />
          <el-option label="人际关系" value="8" />
          <el-option label="自我认知" value="9" />
          <el-option label="学习能力" value="10" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="关于测评">
        <el-input v-model="testData.aboutTest" placeholder="请输入关于测评内容"></el-input>
      </el-form-item>
      <el-form-item label="你将获得">
        <el-input v-model="testData.willGain" placeholder="请输入测评收获"></el-input>
      </el-form-item>
      <el-form-item label="适合人群">
        <el-input v-model="testData.suitPlan" placeholder="请输入适合人群"></el-input>
      </el-form-item>
      <el-form-item label="参考文献">
        <el-input v-model="testData.references" placeholder="请输入参考文献"></el-input>
      </el-form-item>
      <el-form-item label="原价">
        <el-input v-model="testData.originPrice" placeholder="请输入原价"></el-input>
      </el-form-item>
      <el-form-item label="折扣价">
        <el-input v-model="testData.discountPrice" placeholder="请输入折扣价"></el-input>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancle">
          取 消
        </el-button>
        <el-button type="primary" @click="confirm">
          确 定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ElMessage, ElMessageBox } from 'element-plus';
import { getCurrentInstance, onMounted, reactive, ref } from 'vue';

const queryInfo = reactive({
  problemName: '',
  pageNum: 1,
  pageSize: 5
});

// 总数据量
const total = ref(0);

const axios = getCurrentInstance().appContext.config.globalProperties.axios;

const queryDataList = () => {
  axios.get('testProblem/page', { // 假设接口为 test/page，需根据实际后端接口调整
    params: queryInfo
  }).then((resp) => {

    if ("resp.code === 200") {
      dataList.value = resp.list;
      total.value = resp.total;
    } else {
      ElMessage.error('数据查询失败');
    }
  });
};

onMounted(() => {
  queryDataList();
});

const dataList = ref([]);

const multipleSelection = ref([]);

const handleSelectionChange = (val : any[]) => {
  multipleSelection.value = val.map(item => item.id);
  console.log(multipleSelection.value);
};

const handleSizeChange = () => {
  queryDataList();
};
const handleCurrentChange = () => {
  queryDataList();
};

// 动态标题
const title = ref('');
// 控制对话框是否显示
const dialogFormVisible = ref(false);
// 新增和修改数据集
const testData = ref({
  // intro: '',
  // counselorId: '',
  // aboutTest: '',
  // willGain: '',
  // suitPlan: '',
  // references: '',
  // originPrice: '',
  // discountPrice: '',
  // testName: '',
  // id: ''
  id:'',
  problemName:'',
  mindTestId:''
});

const cancle = () => {
  testData.value = {
    problemName:'',
    mindTestId:'',
    id:''

  };
  dialogFormVisible.value = false;
};

const confirm = () => {
  console.log('confirm 函数执行，数据：', testData.value);
  if (testData.value.id) {
    axios.put('testProblem', testData.value) // 假设编辑接口为 test，需根据实际调整
        .then((resp) => {
          if ('resp.code === 200') {
            ElMessage.success('编辑成功');
          } else {
            ElMessage.error('编辑失败');
          }
          queryDataList();
          dialogFormVisible.value = false;
        });
  } else {
    axios.post('testProblem', testData.value) // 假设新增接口为 test，需根据实际调整
        .then((resp) => {
          if ('resp.code === 200') {
            ElMessage.success('新增成功');
          } else {
            ElMessage.error('新增失败');
          }
          queryDataList();
          dialogFormVisible.value = false;
        });
  }
};

const add = () => {
  title.value = '新增';
  dialogFormVisible.value = true;
};

const edit = (row) => {
  title.value = '编辑';
  dialogFormVisible.value = true;

  testData.value = { ...row };
};

const remove = (id) => {
  ElMessageBox.confirm(
      '此操作将永久删除该数据, 是否继续?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    axios.delete(`testProblem/${id}`,{
      params: { id: id } // 明确传递名为"id"的请求参数
    }) // 假设删除接口为 test/{id}，需根据实际调整
        .then((resp) => {
          if ("resp.code === 200") {
            ElMessage.success('删除成功');
          } else {
            ElMessage.error('删除失败');
          }
          queryDataList();
        });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消删除'
    });
  });
};

const batchRemove = () => {
  ElMessageBox.confirm(
      '此操作将永久删除选中数据, 是否继续?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    axios.delete('testProblem', { // 假设批量删除接口为 test/batch，需根据实际调整
      params: {
        ids: multipleSelection.value.join(',')
      }
    }).then(resp => {
      if ('resp.code === 200') {
        ElMessage.success('批量删除成功');
      } else {
        ElMessage.error('批量删除失败');
      }
      queryDataList();
    });
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消删除'
    });
  });
};
</script>

<style scoped>
.el-breadcrumb {
  border-bottom: 1px solid #EEEEEE;
  padding-bottom: 20px;
}

.demo-form-inline .el-input {
  --el-input-width: 150px;
}

.demo-form-inline {
  padding: 10px 0;
}

.el-pagination {
  margin-top: 15px;
}
</style>